package cn.edu.jsu.official.dao.daoImpl;

import cn.edu.jsu.official.dao.VisitorDao;
import cn.edu.jsu.official.pojo.dto.*;
import cn.edu.jsu.official.pojo.vo.AchievementsCountVO;
import cn.edu.jsu.official.pojo.vo.ArticleVO;
import cn.edu.jsu.official.pojo.vo.HistoryVO;
import cn.edu.jsu.official.pojo.vo.MemberVO;
import cn.edu.jsu.official.pojo.vo.PageModel;
import cn.edu.jsu.official.utils.CollectionUtil;
import com.mongodb.client.gridfs.model.GridFSFile;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.DateOperators;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Repository;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Mo
 * @createTime 2022/3/2 23:11
 * @description
 */
@Slf4j
@Repository
public class VisitorDaoImpl implements VisitorDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private GridFsTemplate gridFsTemplate;

    @Override
    public List<ProjectAchievement> findAllProjectAchievement() {
        List<ProjectAchievement> list = mongoTemplate.findAll(ProjectAchievement.class);
        return list;
    }

    @Override
    public List<MemberInfo> findAllMemberInfo() {
        List<MemberInfo> list = mongoTemplate.findAll(MemberInfo.class);
        return list;
    }

    @Override
    public List<ContestAchievement> findAllContestAchievement(){
        List<ContestAchievement> list=mongoTemplate.findAll(ContestAchievement.class);
        return list;
    }

    @Override
    public List<CopyrightInfo> findAllCopyright() {
        List<CopyrightInfo> list = mongoTemplate.findAll(CopyrightInfo.class);
        return list;
    }
    @Override
    public List<ProjectInProgressInfo> findAllProjectInProgress() {
        List<ProjectInProgressInfo> list=mongoTemplate.findAll(ProjectInProgressInfo.class);
        return list;
    }

    @Override
    public List<GoalInfo> findAllGoalInfo() {
        List<GoalInfo> list=mongoTemplate.findAll(GoalInfo.class);
        return list;
    }

    @Override
    public List<ContestInfo> findAllProjectInfo() {
        List<ContestInfo> list = mongoTemplate.findAll(ContestInfo.class);
        return list;
    }

    @Override
    public List<HistoryProcess> findAllHistoryProcess() {
        List<HistoryProcess> list = mongoTemplate.findAll(HistoryProcess.class);
        return list;
    }

    @Override
    public ArticleVO queryArticle(String articleId) {
        Query query = new Query(Criteria.where("_id").is(articleId));
        ArticleVO one = null;
        try {
            one = mongoTemplate.findOne(query, ArticleVO.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return one;
    }

    @Override
    public GridFSFile queryImage(String imageId) {
        Query query = new Query(Criteria.where("_id").is(imageId));
        GridFSFile one = gridFsTemplate.findOne(query);
        return one;
    }

    @Override
    public List<MemberVO> queryAllMemberWithImage(Integer condition) {
        Integer type = null;
        Query query = null;
//        int count = (int) mongoTemplate.count(query, MemberInfo.class);
//        memberVOPageModel.setRowCount(count);
////        query.with( Sort.by(Sort.Order.asc("_id")));
//        query.skip(memberVOPageModel.getSkip()).limit(memberVOPageModel.getPageSize());
        switch (condition){
            case 1 : type=0; break;
            case 2 : type=1; break;
            case 3 : type=2; break;
            default: break;
        }
        if(type==0){
            query = new Query(Criteria.where("is_active").in(0,1));
        } else if (type == 1){
            query = new Query(Criteria.where("is_active").is(1));
        } else if (type == 2) {
            query = new Query(Criteria.where("is_active").is(0));
        }
        List<MemberInfo> memberInfoList = mongoTemplate.find(query, MemberInfo.class);
        if (CollectionUtils.isNotEmpty(memberInfoList)) {
            List<MemberVO> memberVOList = memberInfoList.stream().map(memberInfo -> {
                MemberVO memberVO = new MemberVO();
                memberVO.setMemberId(memberInfo.getMemberId());
                memberVO.setMemberImageId(memberInfo.getMemberImageId());
                memberVO.setMemberName(memberInfo.getMemberName());
                memberVO.setMemberIntroduction(memberInfo.getMemberIntroduction());
                memberVO.setDepartment(memberInfo.getDepartment());
                memberVO.setMajor(memberInfo.getMajor());
                memberVO.setIsGraduated(memberInfo.getIsGraduated());
                memberVO.setIsActive(memberInfo.getIsActive());
                memberVO.setJoinDate(memberInfo.getJoinDate());
                memberVO.setGraduationDestination(memberInfo.getGraduationDestination());
                memberVO.setPersonalAchievement(memberInfo.getPersonalAchievement());
                memberVO.setPersonalTechStack(memberInfo.getPersonalTechStack());
                memberVO.setIsShown(memberInfo.getIsShown());
                return memberVO;
            }).collect(Collectors.toList());
//            memberVOPageModel.setData(memberVOList);
            return memberVOList;
        }
        return null;
    }

    @Override
    public List<AchievementsCountVO> countAchievementNumberGroupByYears(Integer queryType) {
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.project("completed_date").and(DateOperators.DateToString.dateOf("completed_date").toString("%Y")).as("completedDate"),
                Aggregation.group("completedDate").count().as("count")
                            .last("completedDate").as("completedYear"),
                Aggregation.project("completedYear", "count"),
                Aggregation.sort(Sort.Direction.DESC, "completedYear")
        );
        AggregationResults<AchievementsCountVO> results = null;
        if (queryType == 1) {
            log.info("查询类型: 项目成就");
            results = mongoTemplate.aggregate(aggregation, ProjectAchievement.class, AchievementsCountVO.class);
        } else if (queryType == 2){
            log.info("查询类型: 竞赛成就");
            results = mongoTemplate.aggregate(aggregation, ContestAchievement.class, AchievementsCountVO.class);
        } else if (queryType == 3){
            log.info("查询类型: 软著");
            results = mongoTemplate.aggregate(aggregation, CopyrightInfo.class, AchievementsCountVO.class);
        }
        List<AchievementsCountVO> mappedResults = results.getMappedResults();
        System.out.println(mappedResults);
        return mappedResults;
    }

    @Override
    public List<TechStackInfo> queryAllTechStackInfo() {
        List<TechStackInfo> all = mongoTemplate.findAll(TechStackInfo.class);
        return all;
    }

    @Override
    public List<HistoryVO> findAllHistoryVO() {
        List<HistoryProcess> list = mongoTemplate.findAll(HistoryProcess.class);
        return null;
    }

    @Override
    public List<MemberInfo> queryChooseMember() {
        Query query = new Query(Criteria.where("isShown").is(1));
        List<MemberInfo> list = mongoTemplate.find(query,MemberInfo.class);
        return list;
    }
}
